package com.cskaoyan;


import java.sql.*;

/**
 * 创建日期: 2021/11/23 11:07
 *
 * @author ciggar
 * 查询的Demo
 */
public class JDBCDemo6 {

    public static void main(String[] args) throws SQLException {

        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;

        try {

            // 1. 注册驱动
//            DriverManager.registerDriver(new Driver());
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }

            // 2. 获取连接  返回的是Connection接口的实现类
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/36th_rev?useSSL=false", "root", "123456");

            // 3. 获取statement对象 这个对象是用来包装SQL语句为网络请求

            statement = connection.createStatement();

            // 4. 通过statement对象发送SQL语句
            resultSet = statement.executeQuery("select id as uid,name,class,score from student");
            // 5. 解析结果集

            // 把游标移动到最后
            resultSet.afterLast();

            // 向前移动一位
            resultSet.previous();


            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            String aClass = resultSet.getString("class");
            int score = resultSet.getInt("score");

            System.out.println("id:" + id + ", name:" + name + ", class:" + aClass + ",score:" + score);

        }finally {
            // 6. 关闭资源
            try {
                resultSet.close();
            }finally {
                try {
                    statement.close();
                }finally {
                    connection.close();
                }
            }

        }



    }
}
